package com.auction.mapper;

import com.auction.dto.AuctionPageQueryDTO;
import com.auction.pojo.Auction;
import com.auction.vo.AuctionVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface AuctionMapper {

        /**
         * 分页查询拍卖品列表
         * 
         * @param auctionPageQueryDTO
         * @return
         */
        Page<Auction> pageQuery(AuctionPageQueryDTO auctionPageQueryDTO);

        /**
         * 根据ID查询拍卖品
         *
         * @param id
         * @return
         */
        @Select("SELECT * FROM auction WHERE id = #{id}")
        Auction selectById(@Param("id") Long id);

        /**
         * 插入拍卖品
         *
         * @param auction
         */

        @Options(useGeneratedKeys = true, keyProperty = "id")
        void insert(Auction auction);

        /**
         * 更新拍卖品
         *
         * @param auction
         */
        void update(Auction auction);

        /**
         * 获取拍卖品详情
         * 
         * @param id
         * @return
         */
        AuctionVO selectVOById(@Param("id") Long id);

        /**
         * 更新拍卖品状态
         * 
         * @param id
         * @param status
         * @return
         */
        @Update("UPDATE auction SET status = #{status} WHERE id = #{id}")
        int updateStatus(@Param("id") Long id, @Param("status") String status);

        /**
         * 获取最新拍卖品列表
         * 
         * @param limit
         * @return
         */
        List<AuctionVO> selectLatestAuctions(@Param("limit") int limit);

        /**
         * 根据分类id查询拍卖品列表
         * 
         * @param categoryId
         * @return
         */
        List<AuctionVO> list(@Param("categoryId") Long categoryId);

        /**
         * 删除拍卖品
         * 
         * @param id
         */
        @Delete("DELETE FROM auction WHERE id = #{id}")
        void delete(Long id);

        /**
         * 查询已结束但状态仍为活跃的拍卖
         * 
         * @param now 当前时间
         * @return 已结束的拍卖列表
         */
        @Select("SELECT * FROM auction WHERE status = 'ACTIVE' AND end_time <= #{now}")
        List<Auction> selectEndedActiveAuctions(@Param("now") LocalDateTime now);

        @Update("UPDATE auction SET start_price = #{bidPrice} WHERE id = #{auctionId}")
        void updateCurrentPrice(Long auctionId, BigDecimal bidPrice);
}